package com.homework.note.Controller;

import com.homework.note.Model.Note;
import com.homework.note.Repository.NoteRepository;
import com.homework.note.Service.NoteService;
import com.homework.note.Util.Message;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFSDBFile;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * @author xjx
 * @date 2019/3/10
 */

@RestController
@RequestMapping(value = "note")
public class NoteController {

    @Autowired
    NoteService noteService;

    @Autowired
    NoteRepository noteRepository;


    /**
     *新建笔记
     * @return
     * @throws Throwable
     */
    @PostMapping("create")
    public Message createNote(@RequestParam String fatherID) throws Throwable {
        return Message.success(noteService.createNote(fatherID));
    }

    /**
     * 获取笔记列表
     * @return
     */
    @GetMapping("list")
    public Message noteList(){
        return Message.success(noteService.getNoteList());
    }

    @GetMapping("detail")
    public Message getDetail(@RequestParam String noteID){
        Note note = noteRepository.findById(noteID).get();
        return Message.success(note);
    }

    /**
     * 删除笔记
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public Message deleteNote(@RequestParam String id){
        return Message.success(noteService.deleteNote(id));
    }

    /**
     * 修改笔记
     * @param noteID
     * @param title
     * @param content
     * @return
     */
    @PutMapping("update")
    public Message updateNote(@RequestParam String noteID, @RequestParam String title, @RequestParam String content, @RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
        return Message.success(noteService.updateNote(noteID,title,content,file,request));
    }


    /**@GetMapping("/pic/{id}")
    public void getFile(@PathVariable String id, HttpServletResponse response){
        GridFSFile file = noteService.getById(new ObjectId(id));
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            response.addHeader("Content-Disposition","attachment;filename="+file.getFilename());
            response.addHeader("Content-Length", "" + file.getLength());
            response.setContentType(file.getContentType());
            file.writeTo(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }**/

    /**
     * word格式导出笔记
     */
    @GetMapping("export")
    public Message exportword(@RequestParam String noteID, @RequestParam HttpServletRequest request, @RequestParam HttpServletResponse response){
        return Message.success(noteService.export2word(noteID, request, response));
    }
}
